package com.leo.leetcode.s100;

/**
 *
 * @author xuexiaolei
 * @version 2017年12月01日
 */
public class T100SameTree {
    public static boolean isSameTree(TreeNode p, TreeNode q) {
        if (p==null && q==null){
            return true;
        }else if (p==null || q==null){
            return false;
        }else if (p.val != q.val){
            return false;
        }else if (p.val == q.val && p.right==null && p.left==null && q.left==null && q.right==null){
            return true;
        }
        boolean leftAns = isSameTree(p.left, q.left);
        boolean rightAns = isSameTree(p.right, q.right);
        return leftAns&&rightAns;
    }

    /**
     * 更好的写法
     * @param p
     * @param q
     * @return
     */
    public static boolean isSameTree2(TreeNode p, TreeNode q) {
        if (p==null && q==null)
            return true;
        if (p==null || q==null)
            return false;
        if (p.val==q.val)
            return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
        else
            return false;
    }
}
